#include "chnxml.h"


extern Channel chn[2];

ChnXml::ChnXml()
{

}


bool ChnXml::parseFile(const QString &fileName,QString &error)
{
    QFile file(fileName);
    if(!file.open(QFile::ReadOnly|QFile::Text))
    {
        error="打开通道配置文件失败";
        return false;
    }
    reader.setDevice(&file);


    while(!reader.atEnd())
    {
        reader.readNext();
        if(reader.isStartElement())
        {
            if(reader.name()=="TEP-I-Z")
            {
                readTEPIZ();
            }
        }
    }

    file.close();
    if(reader.hasError())
    {
        error.append("Error:failed to parse file:").append(fileName).append(reader.errorString());
        return false;
    }
    else if(file.error()!=QFile::NoError)
    {
        error.append("Error:cannot reade file:").append(fileName).append(file.errorString());
        return false;
    }
    return true;
}


void ChnXml::readTEPIZ()
{

    while(!reader.atEnd())
    {
        reader.readNext();

        if(reader.isStartElement())
        {
            if(reader.name()=="DEV")
            {
                QString dev_type=reader.attributes().value("dev_type").toString();
                readDEV(dev_type);
            }
        }
    }
}

void ChnXml::readDEV(QString dev_type)
{
    DEV dev;
    dev.dev_type=dev_type;

   // qDebug("Start to read dev:%s",qPrintable(dev_type));
    while(!reader.atEnd())
    {
        reader.readNext();

        if(reader.isStartElement())
        {
            if(reader.name()=="YX")
            {
                YX yx;
                yx.index=reader.attributes().value("index").toString().toInt();
                yx.display=reader.attributes().value("display").toString().toInt();
                yx.name=reader.attributes().value("var_name").toString();
                yx.finvert=reader.attributes().value("finvert").toString().toInt();

               // qDebug("yx :%s",qPrintable(yx.name));

                dev.yxlist<<yx;
            }
            else if(reader.name()=="YC")
            {
                YC yc;
                yc.index=reader.attributes().value("index").toString().toInt();
                yc.display=reader.attributes().value("display").toString().toInt();
                yc.name=reader.attributes().value("var_name").toString();
                yc.unit=reader.attributes().value("var_unit").toString();
                yc.preci=reader.attributes().value("preci").toString().toFloat();
                yc.k_val=reader.attributes().value("k_val").toString().toFloat();
                yc.b_val=reader.attributes().value("b_val").toString().toFloat();
                yc.fup1=reader.attributes().value("fup1").toString().toFloat();
                yc.fdown1=reader.attributes().value("fdown1").toString().toFloat();
                yc.fsave=reader.attributes().value("fsave").toString().toInt();
                yc.tsave=reader.attributes().value("tsave").toString().toInt();
                yc.tlazy=reader.attributes().value("tlazy").toString().toInt();


                //qDebug("yc :%s",qPrintable(yc.name));

                dev.yclist<<yc;
            }
        }
        if(reader.isEndElement())
        {
            if(reader.name()=="DEV")
            {
               // qDebug("End of Dev:%s",qPrintable(dev_type));
                break;
            }
        }




    }
    setDEV(dev);

}


void ChnXml::setDEV(const DEV &dev)
{
    int i=0,j=0;
    for(j=0;j<2;j++)
    {
        for(i=0;i<chn[j].sysdev.size();i++)
        {
            if(chn[j].sysdev.at(i).dev_type==dev.dev_type)
            {
                chn[j].sysdev[i].yxlist.clear();;
                chn[j].sysdev[i].yclist.clear();
                chn[j].sysdev[i].yxlist<<dev.yxlist;
                chn[j].sysdev[i].yclist<<dev.yclist;
                QString str;
                str=QString("dev yx size:%1,yc size:%2").arg(chn[j].sysdev[i].yxlist.size()).arg(chn[j].sysdev[i].yclist.size());
                qDebug(qPrintable(str));
            }
        }
    }
}


bool ChnXml::writeXml(const QString &fileName,QString &error)
{
    int i=0,j=0,k=0;
    QFile file(fileName);

    if(!file.open(QIODevice::WriteOnly))
    {
        error.append("创建channel.cfg时,无法打开文件");
        return false;
    }

    QXmlStreamWriter writer;
    writer.setDevice(&file);

    writer.writeStartDocument();

    writer.writeStartElement("TEP-I-Z");
    writer.writeAttribute("version","1.0");

    for(i=0;i<2;i++)
    {
        QString fac_name;

        //写channel
        writer.writeStartElement("channel");
        writer.writeAttribute("fac_no",QString::number(chn[i].fac_no));
        fac_name=QString("电源系统%1段母线部分").arg(chn[i].fac_no+1);
        writer.writeAttribute("fac_name",fac_name);
        writer.writeAttribute("channel_type","0");
        writer.writeAttribute("rule_type","1");
        writer.writeAttribute("com_name",chn[i].com_name);
        writer.writeAttribute("baud",QString::number(chn[i].baud));
        writer.writeAttribute("data_bit","8");
        writer.writeAttribute("stop_bit","0");
        writer.writeAttribute("parity_bit","0");
        writer.writeAttribute("errup","500");


        for(j=0;j<chn[i].sysdev.size();j++)
        {
            DEV dev=chn[i].sysdev.at(j);

            //写设备
            writer.writeStartElement("DEV");
            writer.writeAttribute("fac_no",QString::number(chn[i].fac_no));
            writer.writeAttribute("index",QString::number(j));
            writer.writeAttribute("dev_name",dev.dev_name);
            writer.writeAttribute("dev_addr",QString::number(dev.devAddr));
            writer.writeAttribute("dev_type",dev.dev_type);
            writer.writeAttribute("isworking","1");
            writer.writeAttribute("page_name","");

            for(k=0;k<dev.yxlist.size();k++)
            {
                YX yx=dev.yxlist.at(k);
                //写yx
                writer.writeStartElement("YX");
                writer.writeAttribute("index",QString::number(yx.index));
                writer.writeAttribute("var_name",yx.name);
                writer.writeAttribute("display",QString::number(yx.display));
                writer.writeAttribute("var_type","");
                writer.writeAttribute("finvert",QString::number(yx.finvert));

                writer.writeEndElement();;

            }

            for(k=0;k<dev.yclist.size();k++)
            {
                YC yc=dev.yclist.at(k);
                //写yx
                writer.writeStartElement("YC");
                writer.writeAttribute("index",QString::number(yc.index));
                writer.writeAttribute("var_name",yc.name);
                writer.writeAttribute("display",QString::number(yc.display));
                writer.writeAttribute("var_unit",yc.unit);
                writer.writeAttribute("preci",QString::number(yc.preci));
                writer.writeAttribute("k_val",QString("%1").arg(yc.k_val));
                writer.writeAttribute("b_val",QString("%1").arg(yc.b_val));
                writer.writeAttribute("fup1",QString("%1").arg(yc.fup1));
                writer.writeAttribute("fdown1",QString("%1").arg(yc.fdown1));
                writer.writeAttribute("fsave",QString::number(yc.fsave));
                writer.writeAttribute("tsave",QString::number(yc.tsave));
                writer.writeAttribute("tlazy",QString::number(yc.tlazy));

                writer.writeEndElement();;

            }


            writer.writeEndElement();
            //结束写设备
        }
        writer.writeEndElement();
        //结束写channel
    }

    writer.writeEndElement();

    writer.writeEndDocument();

    return true;

}

